package cn.tkb.pet.service.impl;

import cn.tkb.basic.service.impl.BaseServiceImpl;
import cn.tkb.pet.domain.Pet;
import cn.tkb.pet.mapper.PetDetailMapper;
import cn.tkb.pet.mapper.PetMapper;
import cn.tkb.pet.service.IPetService;
import cn.tkb.user.domain.LoginInfo;
import cn.tkb.user.domain.User;
import cn.tkb.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 宠物上架实现
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        //1.改状态
        //2.上架时间
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onSaleTime",new Date());
        petMapper.onsale(map);

    }

    /**
     * 宠物下架实现
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        //1.改状态
        //2.下架时间
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onSaleTime",new Date());
        petMapper.offSale(map);
    }

    @Override
    public Pet queryByDetailId(Long petId) {
        return petMapper.loadByIdDetail(petId);
    }

    @Override
    public void adopt(Map<String, Long> param,LoginInfo currentLoginInfo) {
        //1.下架宠物，更新下架时间
        Long petId = param.get("petId");
        Pet pet = petMapper.loadById(petId);
        pet.setState(0);
        pet.setOffsaletime(new Date());
        //2.绑定领养人
        User user = userMapper.loadByLoginInfoId(currentLoginInfo.getId());
        pet.setUser_id(user.getId());
        petMapper.update(pet);
    }
}
